package oldFiles;
import lejos.nxt.*;
import lejos.util.Timer;
/**
 * A simple sonar sensor test program.
 * 
 * The sensor should be connected to port 1. In the
 * known bugs and limitations of leJOS NXJ version alfa_03
 * it is mentioned that a gap of at least 300 msec is 
 * needed between calls of getDistance. This is the reason 
 * for the delay of 300 msec between sonar readings in the 
 * loop.
 * 
 * @author  Ole Caprani
 * @version 30.08.07
 */
public class SoundSensorTest 
{
   private static ADSensorPort port;
   private static final int SAMPLE_INTERVAL = 1; // the default value was 300
   private static final int NUM_OF_SAMPLES = 1000;
   private static final int ADC_MAX = 1023;
   
   public static void main(String [] args)  
   throws Exception 
   {
	   port = SensorPort.S2;
	   TimeoutCounter tc = new TimeoutCounter();
	   tc.setCount(0);
	   Timer t = new Timer(1, tc);
	   
	   DataLogger dl = new DataLogger("sound_test.txt");
       //SoundSensor ss = new SoundSensor(SensorPort.S2);
	   
       LCD.drawString("Sound ", 0, 0);
	   
       int readValue;
       int sampleCounter = 0;
       int msCount;
       
       t.start();
       while (! Button.ESCAPE.isPressed() && sampleCounter <= NUM_OF_SAMPLES)
       {
    	   
    	   //readValue = ss.readValue();
    	   readValue = ADC_MAX - port.readRawValue();
    	   //LCD.drawInt(readValue,3,13,0);
    	   
    	   msCount = tc.getCount();
    	   dl.writeSample(msCount);
    	   dl.writeSample(readValue);
    	   sampleCounter++;
           //Thread.sleep(SAMPLE_INTERVAL);
       }
       t.stop();
       dl.close();
       LCD.clear();
       LCD.drawString("Program stopped", 0, 0);
       Thread.sleep(2000);
   }
}
